package com.joint.web.action.com;

import com.fz.us.base.bean.BaseEnum;
import com.fz.us.base.bean.Pager;
import com.fz.us.base.util.PinyinUtil;
import com.joint.base.entity.Company;
import com.joint.base.entity.Users;
import com.joint.base.service.UsersService;
import com.joint.base.util.DataUtil;
import com.joint.core.entity.Announce;
import com.joint.core.service.AnnounceService;
import com.joint.web.action.BaseAdminAction;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by gcw on 2015/3/5.
 */
@ParentPackage("com")
public class AjaxAnnounceAction extends BaseAdminAction {
    @Resource
    private AnnounceService announceService;
    @Resource
    private UsersService usersService;

    private List<Announce> announceList;
    private Set<Users> usersSet;
    private Announce announce;
    private String publishTime;
    private String theme;
    private String content;
    private String userId;
    private String callType;

    public String list(){
        pager = new Pager();
        pager.setPageSize(rows);
        pager.setPageNumber(page);
        pager.setOrderBy("createDate");
        pager.setOrderType(BaseEnum.OrderType.desc);
        if(StringUtils.isNotEmpty(sidx)&&BaseEnum.OrderType.valueOf(sord)!=null){
            pager.setOrderBy(sidx);
            pager.setOrderType(BaseEnum.OrderType.valueOf(sord));
        }
        Company com = usersService.getCompanyByUser();
        pager = announceService.findByPagerAndCompany(pager, null, com, new BaseEnum.StateEnum[]{BaseEnum.StateEnum.Enable});
        announceList = (List<Announce>) pager.getList();
        List<JSONObject> dataRows=new ArrayList<JSONObject>();
        Map<String, Object> data = new HashMap<String, Object>();
        Map<String,Object> rMap;
        for(Announce announce:announceList){
            rMap = new HashMap<String, Object>();
            rMap.put("id",announce.getId());
            rMap.put("publishTime", DataUtil.DateToString(announce.getPublishTime()));
            rMap.put("theme",announce.getName());
            rMap.put("content",announce.getContent());
            rMap.put("createDate", DataUtil.DateToString(announce.getCreateDate(), "yyyy-MM-dd"));
            dataRows.add(JSONObject.fromObject(rMap));
        }
        long total =pager.getTotalCount();
        if(total%rows==0){
            total = total/rows;
        }else{
            if(total<rows){
                total = 1;
            }else{
                total = total/rows + 1;
            }
        }
        data.put("dataRows",dataRows);
        data.put("page",page);
        data.put("rows",rows);
        data.put("total",total);
        data.put("records",pager.getTotalCount());
        return  ajaxJson(JSONObject.fromObject(data).toString());
    }
    public String annList(){
        Pager pager = new Pager(0);
        Company com = usersService.getCompanyByUser();

        pager = announceService.findByPagerAndCompany(pager, null, com, new BaseEnum.StateEnum[]{BaseEnum.StateEnum.Enable});
        announceList = (List<Announce>) pager.getList();

        return "annList";
    }

    public String save(){
        if(StringUtils.isNotEmpty(keyId)){
            announce=announceService.get(keyId);
        }else{
            announce=new Announce();
        }
        if(StringUtils.equals("all",callType)){
            announce.setCallType(0);
        }else{
            announce.setUsersSet(getUsersSpit(userId));
            announce.setCallType(1);
        }
        announce.setPublishTime(DataUtil.StringToDate(publishTime, "yyyy-MM-dd"));
        announce.setContent(content);
        announce.setName(theme);
        announce.setPinYin(PinyinUtil.getPingYin(theme));
        announce.setPinYinHead(PinyinUtil.getPinYinHeadChar(theme));
        announce.setState(BaseEnum.StateEnum.Enable);
        announce.setCompany(usersService.getCompanyByUser());
        announce.setCreater(usersService.getLoginInfo());
        announceService.updateAndEnable(announce);
        ecPushLogService.sendAnnouncementNotification(announce);
        return ajaxHtmlCallback("200", "保存成功！","操作状态");
    }

    public String input(){
        pager=new Pager();
        Company company=usersService.getCompanyByUser();
        if(StringUtils.isNotEmpty(keyId)){
            announce=announceService.get(keyId);
            if(announce.getCallType()==0){
                pager= usersService.findByPagerAndCompany(pager,null,company,new BaseEnum.StateEnum[]{BaseEnum.StateEnum.Enable});
                List<Users> usersList= (List<Users>) pager.getList();
                usersSet = new HashSet<Users>();
                usersSet.addAll(usersList);
            }else{
                usersSet=announce.getUsersSet();
                if(usersSet == null) usersSet = new HashSet<Users>();
            }
        }
        return "input";
    }

    public String delete(){
        if(StringUtils.isNotEmpty(keyId)){
            announce=announceService.get(keyId);
            announce.setState(BaseEnum.StateEnum.Delete);
        }
        return ajaxHtmlCallback("200", "删除成功！","操作状态");
    }

    private Set<Users> getUsersSpit(String str){
        Set<Users> usersSet=new HashSet<Users>();
        String[] ary=str.split(",");
        for(String item:ary){
            if(StringUtils.isNotEmpty(item)) {
                usersSet.add(usersService.get(item));
            }
        }
        return  usersSet;
    }
    public List<Announce> getAnnounceList() {
        return announceList;
    }

    public void setAnnounceList(List<Announce> announceList) {
        this.announceList = announceList;
    }

    public Announce getAnnounce() {
        return announce;
    }

    public void setAnnounce(Announce announce) {
        this.announce = announce;
    }

    public String getPublishTime() {
        return publishTime;
    }

    public void setPublishTime(String publishTime) {
        this.publishTime = publishTime;
    }

    public String getTheme() {
        return theme;
    }

    public void setTheme(String theme) {
        this.theme = theme;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public Set<Users> getUsersSet() {
        return usersSet;
    }

    public void setUsersSet(Set<Users> usersSet) {
        this.usersSet = usersSet;
    }

    public String getCallType() {
        return callType;
    }

    public void setCallType(String callType) {
        this.callType = callType;
    }
}
